/**
 * Copyright 2011 FeedDreamwork SIG
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.feeddreamwork.processors;

public final class FeedProcessorBuilder {
	private static final String PARAMETER_SPLITER = "##";

	public static FeedProcessor getProcessor(String command) {
		if (command.startsWith("addafter"))
			return new AddAfterProcessor(command.replace("addafter ", ""));
		if (command.startsWith("addbefore"))
			return new AddBeforeProcessor(command.replace("addbefore ", ""));
		if (command.startsWith("createfeed")) {
			String[] parameters = command.replace("createfeed ", "").split(
					PARAMETER_SPLITER);
			return new CreateFeedProcessor(parameters[0], parameters[1]);
		}
		if (command.startsWith("fetchfeed"))
			return new FetchFeedProcessor(command.replace("fetchfeed ", ""));
		if (command.startsWith("fetchpage"))
			return new FetchPageProcessor();
		if (command.startsWith("getlatest"))
			return new GetLatestProcessor(Integer.parseInt(command.replace(
					"getlatest ", "")));
		if (command.startsWith("linkfilter"))
			return new LinkFilterProcessor(command.replace("linkfilter ", ""));
		if (command.startsWith("mergefeed"))
			return new MergeFeedProcessor(command.replace("mergefeed ", ""));
		if (command.startsWith("pubsubhubbub"))
			return new PubsubhubbubProcessor();
		if (command.startsWith("removeafter")
				&& !command.startsWith("removeafter incself"))
			return new RemoveAfterProcessor(
					command.replace("removeafter ", ""), false);
		if (command.startsWith("removeafter incself"))
			return new RemoveAfterProcessor(command.replace(
					"removeafter incself ", ""), true);
		if (command.startsWith("removebefore")
				&& !command.startsWith("removebefore incself"))
			return new RemoveBeforeProcessor(command.replace("removebefore ",
					""), false);
		if (command.startsWith("removebefore incself"))
			return new RemoveBeforeProcessor(command.replace(
					"removebefore incself ", ""), true);
		if (command.startsWith("removeredundancy"))
			return new RemoveRedundancyProcessor();
		if (command.startsWith("replace first")) {
			String[] parameters = command.replace("replace first ", "").split(
					PARAMETER_SPLITER);
			if (parameters.length == 1)
				return new ReplaceProcessor(parameters[0], "", true);
			return new ReplaceProcessor(parameters[0], parameters[1], true);
		}
		if (command.startsWith("replace")) {
			String[] parameters = command.replace("replace ", "").split(
					PARAMETER_SPLITER);
			if (parameters.length == 1)
				return new ReplaceProcessor(parameters[0], "", false);
			return new ReplaceProcessor(parameters[0], parameters[1], false);
		}
		if (command.startsWith("setmetadata")) {
			String[] parameters = command.replace("setmetadata ", "")
					.split(" ");
			if (parameters.length == 1)
				return new SetMetadataProcessor(parameters[0], null);
			return new SetMetadataProcessor(parameters[0], parameters[1]);
		}
		if (command.startsWith("sortentry"))
			return new SortEntryProcessor();
		if (command.startsWith("timefilter"))
			return new TimeFilterProcessor(command.replace("titlefilter ", ""));
		if (command.startsWith("titlefilter"))
			return new TitleFilterProcessor(command.replace("titlefilter ", ""));

		if (command.startsWith("mergeecnufeed"))
			return new MergeEcnuFeedProcessor(command.replace("mergeecnufeed ",
					""));
		return null;
	}

	private FeedProcessorBuilder() {
	}
}
